package com.zhuizikejithreeig.cn.threeigkeji.loginfo;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;

import com.zhuizikejithreeig.cn.been.MacBeen;


import com.zhuizikejithreeig.cn.been.MacErrBeen;
import com.zhuizikejithreeig.cn.content.ContectUtils;
import com.zhuizikejithreeig.cn.content.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.MainActivity;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;

import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;


import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

import kr.co.namee.permissiongen.PermissionGen;
import okhttp3.Call;
import okhttp3.MediaType;

public class SaoMaActivity extends BasicActivity implements QRCodeView.Delegate {

    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;

    private QRCodeView mQRCodeView;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showBody();
        setContentView(R.layout.activity_sao_ma);
        PermissionGen.with(SaoMaActivity.this)
                .addRequestCode(REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY)
                .permissions(
                        Manifest.permission.CAMERA
                )
                .request();

        mQRCodeView = (ZBarView) findViewById(R.id.zxingview);
        mQRCodeView.setDelegate(this);
        mPhone = Utils.getString(ContectUtils.PHONESAVE);
//        if (TextUtils.isEmpty(mPhone)&&!Utils.isPhoneNO(mPhone)){
//            skipOtherActivity(LoginActivity.class,true);
//            CompontApplayCation.loginNeedFinsh = false;
//            return;
//        }

    }


    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();
        //        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);

        mQRCodeView.showScanRect();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //开始扫描二维码
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate(String result) {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
        mQRCodeView.startSpot();
        if (result != null || !TextUtils.isEmpty(result)) {
            mQRCodeView.stopSpot();
            sendMac(result);

        }
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        //        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        vibrate(result);

    }

    private void sendMac(String mac) {
        showProgrees(getResources().getString(R.string.string60));
        HashMap<String, String> map = new HashMap<>();
        map.put("phone",mPhone);
        map.put("mac", mac);
        map.put("position",  CompontApplayCation.postion);
        System.out.println( map);
        System.out.println( map);
        System.out.println( map);
        String json = GsonUtil.parseMapToJson(map);
        showProgrees(getResources().getString(R.string.string60));
        OkHttpUtils
                .postString()
                .url(UriContect.INDEX)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        hideProgree();
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        //开始扫描二维码
                        mQRCodeView.startSpot();

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        System.out.println("response"+response);
                        System.out.println("response"+response);
                        System.out.println("response"+response);
                        MacBeen been = GsonUtil.parseJsonToBean(response, MacBeen.class);
                        MacErrBeen beenErr = GsonUtil.parseJsonToBean(response, MacErrBeen.class);
                        if (been == null) {
                            //开始扫描二维码

                            if (beenErr!=null){
                                ToastMake.showToast(beenErr.getMsg());
                            }
                            mQRCodeView.startSpot();
                            return;
                        }
                        if (!TextUtils.equals(been.getError_code(), "0")) {
                            ToastMake.showToast(been.getMsg());
                            //开始扫描二维码
                            mQRCodeView.startSpot();
                            return;
                        } else {
                            //扫描成功后停止扫描
                            mQRCodeView.stopSpot();
                            if (been.getData().getStatus()!=null){
                                Utils.setString(been.getData().getStatus(), ContectUtils.STATUS);
                            }else {
                                Utils.setString("4", ContectUtils.STATUS);
                            }
                            //扫描出来二维码,向后台传递二维码的信息
//                            initData(been.getMsg());
                            CompontApplayCation.formLgin = true;
//                            CompontApplayCation.postion = null;
//                            skipOtherActivity(MainActivity.class,true);
                            //进入主界面的时候,清空所有的任务栈
                            Intent intent = new Intent(SaoMaActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();

                        }
                    }
                });

    }


    @Override
    public void onScanQRCodeOpenCameraError() {
        //        Log.e(TAG, "打开相机出错");
    }



}
